/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment.process;

import assignment.jaxb.products.ProductType;
import assignment.jaxb.products.Products;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author HoangVu
 */
public class productSAXProcessor implements Serializable {
    Products products = new Products();

    String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url="jdbc:sqlserver://localhost:1433;instanceName=SQLEXPRESS;databaseName=ShoppingDB";
     public Connection getConnection() {
         Connection con = null;
        try {
         //   Properties prop = new Properties();
         //   prop.load(new FileInputStream(fileName));
           // String driver = prop.getProperty("driver");
           // String url = prop.getProperty("url");
            Class.forName(driver);
            con = DriverManager.getConnection(url, "sa", "sa");
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(productSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(productSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);       
        } 
        return con;
    }

     public productSAXProcessor(){
        try {
            Connection con = getConnection();
            String sql = "SELECT * FROM Product WHERE isactive = 1";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);

             while (rs.next()) {
                ProductType product = new ProductType();
                int id = rs.getInt("id");
                product.setId(new BigInteger(String.valueOf(id)));

                product.setProductName(rs.getString("productname"));

                float price = rs.getFloat("price");
                product.setPrice(price);
                product.setQuantity(new BigInteger(rs.getString("quantity")));
                product.setImageThumbnail(rs.getString("imageThumbnail"));
                int categoryid = rs.getInt("categoryid");
                product.setCategoryid(new BigInteger(String.valueOf(categoryid)));
                products.getProduct().add(product);
            }

        } catch (SQLException ex) {
            Logger.getLogger(productSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }
     }

     public void marshallXMLProduct(String filename)
    {
        try {
            JAXBContext ctx = JAXBContext.newInstance(Products.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(products, new File(filename));
        } catch (JAXBException ex) {
            Logger.getLogger(productSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
